package cn.abcsys.cloud.devops.web.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

//import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.abcsys.cloud.devops.web.dao.EnvMapper;
import cn.abcsys.cloud.devops.web.dao.UserMapper;
import cn.abcsys.cloud.devops.web.dao.UserRoleEnvProjMapper;
import cn.abcsys.cloud.devops.web.entity.Env;
import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.GridBeanAndData;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.entity.User;
import cn.abcsys.cloud.devops.web.service.UserService;
/**
 * 用户服务实现类
 * @author xzg
 *
 */
@Component
public class UserServiceImpl implements UserService{
	private static final Logger LOGGER = Logger.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;
	/*@Resource 
	private EnvUserMapper envUserMapper;*/
	@Autowired
	private EnvMapper envMapper;
	@Resource
	private UserRoleEnvProjMapper userRoleMapper;
	
	@Override
	public Map<String,Object> getUserDetail(Map<String,Integer> map) {
		
		Map<String, Object> userInfo = null;
		try {
			userInfo = userMapper.getUserDetail(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("查询用户详细环境角色信息失败"+e.getMessage());
		}
		return userInfo;
	}

	@Override
	public GridBean getUsers(Map<String, Object> map,PagerModel pagerModel) {
		int page = pagerModel.getPage();
		int rows = pagerModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<User> pageObject = new Page<User>();
		PageHelper.startPage(page, rows);
		List<User> userList = userMapper.getUserList(map);
		for(User usr: userList){
			pageObject.add(usr);
		}
		totalpage = ((Page<?>) userList).getPages();
		totalNum = ((Page<?>) userList).getTotal();
		GridBean gridBean = new GridBean(page, totalpage, totalNum.intValue(), pageObject);
		return gridBean;
	}

	@Override
	public int insert(User user) {
		return userMapper.insert(user);
	}

	/**
	 * 删除用户以及相关联的表
	 */
	@Transactional
	@Override
	public void delete(Integer userId) {
		try{
			 //删除用户
			userMapper.deleteUserById(userId);
			/*//删除用户角色表
			userRoleMapper.deleteByUserId(userId);
			//删除用户环境表
			envUserMapper.deleteEnvByUserId(userId);
			//删除用户项目表
			userMapper.deleteUserProject(userId);*/
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Override
	public int updateUserByUserId(User user) {
		try{
			return userMapper.updateUserByUserId(user);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		 return 0;
	}

	@Override
	public User getUserByUid(String uid) {
		return userMapper.getUserByUid(uid);
	}

	@Override
	public int updateByUid(User ldapUser) {
		try {
			return userMapper.updateUserByUid(ldapUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public User getUserByUserId(Integer userId) {
		return userMapper.getUser(userId);
	}

	@Override
	public List<Env> getEnv(User user) {
		// TODO Auto-generated method stub
 		List<Env> list = new ArrayList<Env>();
		int isAdmin = user.getIsAdmin();
		if(1==isAdmin)
			list = envMapper.getAllEnv();
		else
			try {
				list =  envMapper.getEnvByUserId(user.getUserId());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				LOGGER.error(user.getUserId()+"数据库操作异常！");
				e.printStackTrace();
			}
		
		return list;
	}

	/**
	 * 超级管理员查看环境管理员
	 */
	@Override
	public List<User> getEnvUser() {
		// TODO Auto-generated method stub
		return userMapper.getEnvRoleUser();
	}

	/**
	 * 查看相应环境下的普通用户
	 */
	@Override
	public List<User> getOrdinaryleUser(Integer envId) {
		// TODO Auto-generated method stub
		
		return userMapper.getOrdinaryleUser(envId);
	}

	/**
	 * 查看相应环境下的项目管理员
	 */
	@Override
	public List<User> getProjectUser(Integer envId) {
		
		return userMapper.getProjectRoleUser(envId);
	}

	@Override
	public boolean checkUser(User user) {
		// TODO Auto-generated method stub
		User isExit = null;
		isExit = userMapper.checkUsers(user);
		if(null == isExit)
			return false;
		
		return true;
	}

	@Override
	public int lockUser(Integer userId) {
		// TODO Auto-generated method stub
		try {
			return userMapper.lockUserByid(userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return 0;
		}
	}

	@Override
	public List<User> selectEffectUsers() {
		// TODO Auto-generated method stub
		
		return userMapper.getUsers();
	}
	
	@Override
	public GridBeanAndData getSelectUsers(Map<String, Object> map,PagerModel pagerModel) {
		int page = pagerModel.getPage();
		int rows = pagerModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<User> pageObject = new Page<User>();
		PageHelper.startPage(page, rows);
		List<User> userList = userMapper.getUserList(map);
		for(User usr: userList){
			pageObject.add(usr);
		}
		totalpage = ((Page<?>) userList).getPages();
		totalNum = ((Page<?>) userList).getTotal();
		List<User> selectUserList = userMapper.getSelectUsers(map);
		GridBeanAndData gridBean = new GridBeanAndData(page, totalpage, totalNum.intValue(), pageObject,selectUserList);
		return gridBean;
	}
}
